/*
 *  Copyright 2017 Google Inc. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.google.blockly.ohos.ui;

import com.google.blockly.ohos.ResourceTable;
import com.google.blockly.utils.LangUtils;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.Optional;

/**
 * Default dialog window shown when creating or renaming a variable in the workspace.
 */
public class NameVariableDialog extends CommonDialog {
    private String mVariable;
    private Callback mClickListener;
    private TextField mNameEditText;
    private boolean mIsRename;
    private Component nameView;
    private Context mContext;

    public NameVariableDialog(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public void onCreate() {
        LayoutScatter inflater = LayoutScatter.getInstance(mContext);
        nameView = inflater.parse(ResourceTable.Layout_name_variable_view, null, false);
        mNameEditText = (TextField) nameView.findComponentById(ResourceTable.Id_name);
        mNameEditText.setText(mVariable);

        Text title = (Text) nameView.findComponentById(ResourceTable.Id_title);
        title.setText(LangUtils.interpolate("%{BKY_IOS_VARIABLES_VARIABLE_NAME}"));

        Text description = (Text) nameView.findComponentById(ResourceTable.Id_description);
        if (mIsRename) {
            description.setText(
                    LangUtils.interpolate("%{BKY_RENAME_VARIABLE_TITLE}").replace("%1", mVariable));
        } else {
            description.setText(LangUtils.interpolate("%{BKY_NEW_VARIABLE_TITLE}"));
        }

        Button btnOk = (Button) nameView.findComponentById(ResourceTable.Id_btn_ok);
        btnOk.setText(LangUtils.interpolate("%{BKY_IOS_OK}"));
        btnOk.setClickedListener(component -> {
            if (mClickListener != null) {
                mClickListener.onNameConfirmed(mVariable,
                        mNameEditText.getText());
            }
            hide();
        });
        Button btnCancel = (Button) nameView.findComponentById(ResourceTable.Id_btn_cancel);
        btnCancel.setText(LangUtils.interpolate("%{BKY_IOS_CANCEL}"));
        btnCancel.setClickedListener(component -> {
            if (mClickListener != null) {
                mClickListener.onNameCanceled(mVariable);
            }
            hide();
        });

        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(mContext);
        DisplayAttributes displayAttributes = display.get().getAttributes();
        int width = displayAttributes.width;

        setContentCustomComponent(nameView);
        setSize((int) (width * 0.85), ComponentContainer.LayoutConfig.MATCH_CONTENT);
    }

    public void setVariable(String variable, final Callback clickListener, boolean isRename) {
        if (clickListener == null) {
            throw new IllegalArgumentException("Must have a listener to perform an action.");
        }
        mVariable = variable;
        mClickListener = clickListener;
        mIsRename = isRename;
    }

    public abstract static class Callback {
        public abstract void onNameConfirmed(String originalName, String newName);

        public void onNameCanceled(String originalName) {
            // Do nothing by default
        }
    }

}
